package com.service.impl;

import com.baomidou.mybatisplus.toolkit.MapUtils;
import com.redis.RedisUtil;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.Map;
import java.util.List;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.utils.PageUtils;
import com.utils.Query;


import com.dao.StoreupDao;
import com.entity.StoreupEntity;
import com.service.StoreupService;
import com.entity.vo.StoreupVO;
import com.entity.view.StoreupView;

import javax.annotation.Resource;


@Service("storeupService")
public class StoreupServiceImpl extends ServiceImpl<StoreupDao, StoreupEntity> implements StoreupService {


    @Resource
    private RedisUtil redisUtil;

    @Resource
    private StoreupDao storeupDao;

    private static final String SUBJECT_LIKED_KEY = "subject.liked";

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<StoreupEntity> page = this.selectPage(
                new Query<StoreupEntity>(params).getPage(),
                new EntityWrapper<StoreupEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Wrapper<StoreupEntity> wrapper) {
        Page<StoreupView> page =new Query<StoreupView>(params).getPage();
        page.setRecords(baseMapper.selectListView(page,wrapper));
        PageUtils pageUtil = new PageUtils(page);
        return pageUtil;
    }


    @Override
    public List<StoreupVO> selectListVO(Wrapper<StoreupEntity> wrapper) {
        return baseMapper.selectListVO(wrapper);
    }

    @Override
    public StoreupVO selectVO(Wrapper<StoreupEntity> wrapper) {
        return baseMapper.selectVO(wrapper);
    }

    @Override
    public List<StoreupView> selectListView(Wrapper<StoreupEntity> wrapper) {
        return baseMapper.selectListView(wrapper);
    }

    @Override
    public StoreupView selectView(Wrapper<StoreupEntity> wrapper) {
        return baseMapper.selectView(wrapper);
    }

    @Override
    public void syncLiked() {
        Map<Object, Object> subjectLikedMap = redisUtil.getHashAndDelete(SUBJECT_LIKED_KEY);
        if (MapUtils.isEmpty(subjectLikedMap)) {
            return;
        }
        //批量同步到数据库
        List<StoreupView> subjectLikedList = new LinkedList<>();
        subjectLikedMap.forEach((key, val) -> {
            StoreupView subjectLiked = new StoreupView();
            String[] keyArr = key.toString().split(":");
            String subjectId = keyArr[0];
            String likedUser = keyArr[1];
            subjectLiked.setRefid(Long.valueOf(subjectId));
            subjectLiked.setUserid(Long.valueOf(likedUser));
            subjectLikedList.add(subjectLiked);
        });
        storeupDao.batchInsertOrUpdate(subjectLikedList);
    }

    @Override
    public void syncLikedByMsg(StoreupView storeupView) {
        List<StoreupView> subjectLikedList = new LinkedList<>();
        StoreupView storeupView1 = new StoreupView();
        storeupView1.setRefid(Long.valueOf(storeupView.getRefid()));
        storeupView1.setUserid(storeupView.getUserid());
        subjectLikedList.add(storeupView1);
        storeupDao.batchInsertOrUpdate(subjectLikedList);
    }


}
